<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout"> 退出 </el-button></el-header
    >
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="pick ? '64px' : '200px'">
        <!-- 折叠按钮 -->
        <div class="toggle-button" @click="packUp">
          {{ pick ? '展开' : '收起' }}
        </div>
        <!-- 侧边栏导航 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="pick" :collapse-transition="false" router :default-active="activePath">
          <el-submenu :index="obj.id + ''" v-for="obj in MenuList" :key="obj.id">
            <template slot="title">
              <i :class="obj.iconclass"></i>
              <span>{{ obj.authName }}</span>
            </template>
            <el-menu-item :index="'/' + item.path" v-for="item in obj.children" :key="item.id" @click="saveNavState('/' + item.path)">
              <template slot="title">
                <i :class="item.iconclass"></i>
                <span>{{ item.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体结构 -->
      <el-main>
        <!-- 路由占位符、home下面的子组件的占位符 -->
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      MenuList: [],
      pick: false,
      activePath: ''
    }
  },
  created() {
    // 异步获取侧边栏列表
    this.getList()
    // 从缓存中取出最后一次停留的页面
    const path = window.sessionStorage.getItem('activePath')
    if (path) {
      this.activePath = path
    }
  },
  methods: {
    //   退出函数
    async logout() {
      try {
        await this.$messagebox.confirm('确认退出?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // 点击确定做的事情
        sessionStorage.clear()
        this.$router.push('/login')
      } catch (error) {
        //   点击取消做的事情
      }
    },
    // 获取列表
    async getList() {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) {
        this.$router.push('/login')
        return this.$message.error(res.meta.msg)
      }
      this.MenuList = res.data
    },
    // 列表折叠
    packUp() {
      this.pick = !this.pick
    },
    // 保存二级目录index到sessionStorage
    saveNavState(path) {
      sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  }
}
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}
.el-menu {
  border-right: none;
}
.toggle-button {
  width: 100%;
  background-color: #333240;
  border: none;
  height: 35px;
  line-height: 35px;
  text-align: center;
  color: #fff;
  cursor: pointer;
}
.toggle-button:hover {
  background-color: #373d41;
  color: #409eff;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.el-submenu__title {
  i {
    margin-right: 5px;
  }
}
</style>
